package com.cloudptt.wp.ui.menu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.ui.more.VersionInfoActivity;

public class MenuActivity extends Activity{

	private final static String TAG = "MenuActivity";
	private TextView activity_title;
	private SettingsAdapter mAdapter;
	private ListView mSettingsListView;
	private int curPos = 0;
	
	public static int[] item_icon_group = {
			R.drawable.settings_user
			,R.drawable.settings_call
			,R.drawable.settings_version
			//,R.drawable.settings_version_update_icon
			//,R.drawable.settings_advanced_icon
			//,R.drawable.settings_timer_icon
			};
	
	public static int[] item_title_group = {
			R.string.menu_addr
			,R.string.menu_call_list
			,R.string.menu_sms
			//,R.string.settings_version_update_title
			//,R.string.settings_advanced_title
			//,R.string.settings_timer_title
			};

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		Window window = getWindow();
		window.addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS | LayoutParams.FLAG_TRANSLUCENT_STATUS);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_layout);

		initView();
		ScreenManager.getScreenManager().pushActivity(this);
	}

	private void initView() {
		activity_title =  (TextView) findViewById(R.id.activity_title);
		activity_title.setText(R.string.menu_str);
		mSettingsListView  = (ListView) findViewById(R.id.settings_list);
		mSettingsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				toAnotherActivity(position);
			}
		});
		mAdapter = new SettingsAdapter();
		mSettingsListView.setAdapter(mAdapter);
		mSettingsListView.setSelection(0);
	}

	@Override
	protected void onDestroy(){
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	private void toAnotherActivity(int pos){
		Intent intent = new Intent();
		switch (pos) {
		case 0:  
			intent = new Intent();
			intent.setClass(this, AddressActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;

		/*case 1:
			intent = new Intent(this, VideoSettingActivity.class);
			startActivity(intent);
			break;*/

//		case 1:
//			intent = new Intent(this, DeviceManagerActivity.class);
//			startActivity(intent);
//			break;

		/*case 3:
			SysApplication app = ((SysApplication) getApplicationContext());
			Product product = app.getProduct();
			product.checkCurVersion(this, true, false, SysApplication.APP_KEY);
			break;

		case 4:
			intent = new Intent(this, AdvancedSettingActivity.class);
            startActivity(intent);
			break;

		case 5:
			intent = new Intent(this, TimerSetting.class);
            startActivity(intent);
			break;*/
		
		default:
			break;
		}
	}
	
	class SettingsAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return item_icon_group.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return item_icon_group[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			ViewHolder viewHolder = new ViewHolder();
			
			if(convertView == null){
				convertView = getLayoutInflater().inflate(R.layout.settings_item_layout, parent, false);
				viewHolder.icon = (ImageView)convertView.findViewById(R.id.settings_item_icon);
				viewHolder.title = (TextView)convertView.findViewById(R.id.settings_item_title);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			
			viewHolder.icon.setImageResource(item_icon_group[position]);
			viewHolder.title.setText(item_title_group[position]);
			
			return convertView;
		}
		
	}
	
	class ViewHolder{
		public ImageView icon;
		public TextView title;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d(TAG, "onKeyDown keyCode "+ keyCode);

//		if (keyCode == KeyEvent.KEYCODE_SOFT_LEFT)
//		{
//			//gotoMenuActivity();
//			return false;
//		}
//		else if (keyCode == KeyEvent.KEYCODE_BACK) {
//			//gotoSettingActivity();
//			return false;
//		}

		if (keyCode == 19)//xia
		{
			curPos = mSettingsListView.getSelectedItemPosition();
			Log.d(TAG, "onKeyDown 19 curPos "+ curPos);
			if(curPos <= 0)
			{
				curPos = item_title_group.length -1 ;
			}
			Log.d(TAG, "onKeyDown 19 after curPos "+ curPos);
			mSettingsListView.setSelection(curPos);
			return true;
		}
		else if (keyCode == 20) //shang
		{
			curPos = mSettingsListView.getSelectedItemPosition();
			Log.d(TAG, "onKeyDown 20 curPos "+ curPos);
			if(curPos >= (item_title_group.length-1))
			{
				curPos = 0;
			}
			Log.d(TAG, "onKeyDown 20 after curPos "+ curPos);
			mSettingsListView.setSelection(curPos);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
